package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.jobduty.AuthJobDutyMenuReqDTO;
import com.yixin.hospital.entity.dto.req.jobduty.JobDutyAddReqDTO;
import com.yixin.hospital.entity.dto.req.jobduty.JobDutyEditReqDTO;
import com.yixin.hospital.entity.vo.DeptAndJobDutyTreeNodeVO;
import com.yixin.hospital.entity.vo.JobDutyVO;
import com.yixin.hospital.service.JobDutyService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/job-duty")
@Api(value = "职务管理", tags = "职务管理")
public class JobDutyController {

    @Resource
    private JobDutyService jobDutyService;

    @GetMapping("/list")
    @ApiOperation(value = "职务列表")
    public ApiResult<List<JobDutyVO>> list(@RequestParam(value = "deptId", required = false) Long deptId) {
        return jobDutyService.allList(deptId);
    }

    @GetMapping("/dept-job-duty-tree")
    @ApiOperation(value = "部门-职务 列表树")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptId",value="部门id", paramType = "query")
    })
    public ApiResult<List<DeptAndJobDutyTreeNodeVO>> treeList(@RequestParam Long deptId) {
       return jobDutyService.deptJobDutyTree(deptId);
    }

    @GetMapping("/dept-job-duty")
    @ApiOperation(value = "根据部门Id返回职务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptId",value="部门id", paramType = "query")
    })
    public ApiResult<?> listByDeptId(@RequestParam Long deptId) {
       return jobDutyService.listByDeptId(deptId);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加职务")
    @Transactional
    public ApiResult add(@Valid @RequestBody JobDutyAddReqDTO jobAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobDutyService.add(jobAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑职务")
    @Transactional
    public ApiResult edit(@Valid @RequestBody JobDutyEditReqDTO jobEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobDutyService.edit(jobEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除职务")
    public ApiResult remove(@PathVariable int id) {
        jobDutyService.removeById(id);
        return ApiResult.success();
    }

    @PostMapping("/auth-job-menu")
    @ApiOperation(value = "授予职务菜单权限")
    public ApiResult authJobMenu(AuthJobDutyMenuReqDTO authJobMenuReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobDutyService.authJobMenu(authJobMenuReqDTO, userName);
    }

    @GetMapping("/parent-job-duty")
    @ApiOperation(value = "获取默认上级职务")
    public ApiResult<?> parentJobDuty(@RequestParam String jobDutyId) {
        return jobDutyService.parentJobDuty(jobDutyId);
    }

}
